<template>
  <!-- 主体区域 -->
  <section id="app">
    <!-- 做添加功能 -->
    <TodoHeader @addTodoName="add"></TodoHeader>
    <!-- 列表展示 -->
    <TodoMain :list="list" @sendId="delList"></TodoMain>
    <!-- 统计+清空 -->
    <TodoFooter
      v-show="list.length > 0"
      :list="list"
      @clearList="clear"
    ></TodoFooter>
  </section>
</template>

<script>
import TodoMain from './components/TodoMain.vue'
import TodoFooter from './components/TodoFooter.vue'
import TodoHeader from './components/TodoHeader.vue'
export default {
  data() {
    return {
      list: JSON.parse(localStorage.getItem('list')) || [
        { id: 1, name: '跑步一公里' },
        { id: 3, name: '游泳100米' },
      ],
    }
  },
  watch: {
    list: {
      deep: true,
      handler(value) {
        localStorage.setItem('list', JSON.stringify(value))
      },
    },
  },
  methods: {
    // 添加
    add(value) {
      // console.log(value)
      this.list.unshift({
        id: +new Date(),
        name: value,
      })
    },
    //删除
    delList(id) {
      // console.log(id)
      this.list = this.list.filter((item) => item.id !== id)
    },
    //清空
    clear() {
      this.list = []
    },
  },
  components: {
    TodoMain,
    TodoFooter,
    TodoHeader,
  },
}
</script>

<style>
</style>